//
//  ShangquanView.m
//  ClassifyShop
//
//  Created by yuyao on 15/3/7.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "CustomButtonsView.h"

@implementation CustomButtonsView

- (id)initWithFrame:(CGRect)frame buttons:(NSArray *)buttons{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        CGRect frame1;

        buttonBackView = [[UIView alloc] init];
        [buttonBackView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"6 选中框.png"]]];
        [self addSubview:buttonBackView];
        
        for (int i=0; i < buttons.count; i++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
            frame1.size.width = 60;
            frame1.size.height = 30;
            if (buttons.count>8) {
                if (i<5) {
                    
                    frame1.size.width = 50;
                    frame1.origin.x = 10+(i%5)*50;
                }else{
                    frame1.size.width = 70;
                    frame1.origin.x = 10+(i%5)*(70);
                  
                }
             
                frame1.origin.y = floor(i/5)*(30+20)+10;
            }else{
                frame1.origin.x = (i%4)*(60+10)+10;
                frame1.origin.y = floor(i/4)*(30+20)+10;
            }
          
            if (i==0) {
                [buttonBackView setFrame:frame1];
            }

            [btn setFrame:frame1];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [btn setTitle:[buttons objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTag:100+i];
            [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:btn];
            
        }
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height-1, frame.size.width, 1)];
        [lineView setBackgroundColor:[UIColor lightGrayColor]];
        [self addSubview:lineView];
        
    }
    return self;
}
- (void)buttonPressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    
    
   // [UIView animateWithDuration:0.3 animations:^{
        [buttonBackView setFrame:button.frame];
  //  }];
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)view;
            if (btn.tag==button.tag) {
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }else{
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

            }
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
